package com.fengxing.ams.web.facade;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fengxing.ams.web.common.IContextManager;
import com.fengxing.ams.web.facade.intf.INotificationFacade;
import com.fengxing.ams.web.facade.mouble.notification.INotificationDAO;
import com.fengxing.ams.web.facade.mouble.notification.NotificationVO;
import com.fengxing.ams.web.facade.mouble.user.UserVO;

@Service
@Transactional
public class NotificationFacade implements INotificationFacade {

	@Autowired
	IContextManager contextManager;
	@Autowired
	INotificationDAO notificationDAO;
	
	@Override
	public Boolean queryUnread() {
		UserVO userVO = this.contextManager.getUserVO();
		Long count = notificationDAO.queryUnread(userVO.getUserId());
		return count > 0;
	}

	@Override
	public List<NotificationVO> queryNew() {
		//获取上次查询时间
		Long time = this.contextManager.getLastQueryNoficationTime();
		UserVO userVO = this.contextManager.getUserVO();
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		this.contextManager.setLastQueryNotificationTime();
		return this.notificationDAO.findNew(userVO.getUserId(), c.getTime());
	}

	@Override
	@Transactional(readOnly = false)
	public List<NotificationVO> queryUnreadList() {
		UserVO userVO = this.contextManager.getUserVO();
//		this.notificationDAO.updateReaded(userVO.getUserId());
		return this.notificationDAO.queryRureadList(userVO.getUserId());
	}

	@Override
	public void markRead(List<Integer> ids) {
		UserVO userVO = this.contextManager.getUserVO();
		this.notificationDAO.updateReaded(userVO.getUserId(), ids);
	}

}
